package br.ufc.si.managedBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import modelo.Imovel;


import resources.ArquivoLink;
import dao.ImovelDao;

@ManagedBean(name = "imovelBean")
@SessionScoped
public class ImovelBean {

	private List<String> listFotos;
	private Imovel imovel;
	private List<Imovel> list;

	public ImovelBean() {
		imovel = new Imovel();
		list = new ArrayList<Imovel>();
		listFotos = new ArrayList<String>();
	}

	public List<Imovel> getListIndex() {
		ImovelDao dao = new ImovelDao();
		return dao.getListIndex(imovel);
	}

	public String visualizarImovel(){
		ImovelDao dao = new ImovelDao();
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		int id = Integer.parseInt(request.getParameter("id"));	
		imovel = dao.find(id);

		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

		FacesContext aFacesContext = FacesContext.getCurrentInstance();
		ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();
		String realPath = context.getRealPath("/");

		String pasta = id + "/";
		File file = new File(realPath + "img/imovel/" + pasta);
		String[] list = file.list();
		if(list != null){
			for (int i = 0; i < list.length; i++) {
				listFotos.add("/img/imovel/" + pasta + list[i]);
				System.out.println("Log de lista de fotos " + "/img/imovel/" + pasta + list[i]);
			}
		}

		return ArquivoLink.pesquisaChave("visualizar_imovel")+"?faces-redirect=true";
	}

	public boolean getPossuiFotos(){
		return listFotos.size() != 0;
	}

	public List<String> getListFotos() {
		return listFotos;
	}

	public void setListFotos(List<String> listFotos) {
		this.listFotos = listFotos;
	}

	public String pesquisaImovel() {
		ImovelDao dao = new ImovelDao();
		list = dao.getListParametro(imovel);
		System.out.println(imovel.getCidade());
		System.out.println(imovel.getBairro());
		System.out.println(imovel.getRua());
		System.out.println(list.size());
		for (Imovel im : list) {
			System.out.println(im.getCidade());
			System.out.println(im.getBairro());
			System.out.println(im.getRua());

		}
		imovel = new Imovel();
		return ArquivoLink.pesquisaChave("pesquisa_imovel")
				+ "?faces-redirect=true";
	}

	public boolean getListaVazia(){
		if(list.size() == 0){
			return true;
		}else{
			return false;
		}
	}

	public List<String> getFotos(){

		List<String> lista = new ArrayList<String>();
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

		FacesContext aFacesContext = FacesContext.getCurrentInstance();
		ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();
		String realPath = context.getRealPath("/");

		String pasta = session.getAttribute("idImovel") + "/";
		File file = new File(realPath + "img/imovel/" + pasta);
		String[] list = file.list();
		if(list != null){
			for (int i = 0; i < list.length; i++) {
				lista.add("/img/imovel/" + pasta + list[i]);
			}
		}
		return lista;
	}

	public String removeFoto(){

		FacesContext aFacesContext = FacesContext.getCurrentInstance();
		ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();
		String realPath = context.getRealPath("/");
		realPath = realPath.substring(0,realPath.length()-1);
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();	
		String caminhoFoto = request.getParameter("foto");

		File arquivo = new File(realPath + caminhoFoto);

		arquivo.delete();

		return "";
	}

	public List<Imovel> getList() {
		return list;
	}

	public void setList(List<Imovel> list) {
		this.list = list;
	}

	public Imovel getImovel() {
		return imovel;
	}

	public void setImovel(Imovel imovel) {
		this.imovel = imovel;
	}

	public ImovelDao getDao(){
		return new ImovelDao();
	}
	
	public List<String> getListCidades(){
	
		ImovelDao dao = new ImovelDao();
		
		return dao.getListCidades();
	}
	
}
